package cn.wolfcode.crm.web.controller;


import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IMenuService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.wolfcode.crm.domain.Menu;

import java.util.List;


/**
 * @author konki
 * @time   2018年6月19日 下午7:03:47
 * @email  konki@qq.com
 * @description 
 */
@Controller
@RequestMapping("menu")
public class MenuController {
    @Autowired
    private IMenuService menuService;

    @RequiresPermissions("menu:index")
    @PermissionName("菜单主页")
    @RequestMapping("")
    public String index(){
        return "menu/list";
    }

    @RequiresPermissions("menu:list")
    @PermissionName("菜单数据")
    @RequestMapping("list")
    @ResponseBody
    public PageResult list(QueryObject qo){
        return menuService.query(qo);
    }

    @RequestMapping("save")
    @ResponseBody
    @RequiresPermissions("menu:save")
    @PermissionName("菜单保存")
    public JsonResult save(Menu entity){
        JsonResult result = new JsonResult();
        try {
            menuService.insert(entity);
        }catch (Exception e){
            e.printStackTrace();
            result.markMsg("操作失败");
        }
        return result;
    }
    @RequestMapping("update")
    @ResponseBody
    @RequiresPermissions("menu:update")
    @PermissionName("菜单更新")
    public JsonResult update(Menu entity){
        JsonResult result = new JsonResult();
        try {
            menuService.updateByPrimaryKey(entity);
        }catch (Exception e){
            e.printStackTrace();
            result.markMsg("操作失败");
        }
        return result;
    }
    @RequestMapping("delete")
    @ResponseBody
    @RequiresPermissions("menu:update")
    @PermissionName("菜单删除")
    public JsonResult delete(Long id){
        JsonResult result = new JsonResult();
        try {
            menuService.deleteByPrimaryKey(id);
        }catch (Exception e){
            e.printStackTrace();
            result.markMsg("操作失败");
        }
        return result;
    }

    @RequestMapping("queryRootMenu")
    @ResponseBody
    public List<Menu> queryRootMenu() {
      return   menuService.queryRootMenu();

    }



}
